package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.List;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.OPBillContentsDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.OPBillContentsTBL;
import lu.med.praxiswahl.wahlumed.model.beans.OPBillsTBL;

/**
 * Session Bean implementation class OPBillContentsDAO
 */
@Stateless
@Local(
{ OPBillContentsDAOInt.class })
public class OPBillContentsDAO implements OPBillContentsDAOInt
{

    @PersistenceContext(unitName = "gynaekomedic-ejbPU")
    private EntityManager em = null;

    /**
     * Default constructor.
     */
    public OPBillContentsDAO()
    {
	// TODO Auto-generated constructor stub
    }

    @Override
    public void delete(Long todo)
    {
	OPBillContentsTBL billContent = em.find(OPBillContentsTBL.class, todo);
	if (billContent != null)
	{
	    em.remove(billContent);
	    em.flush();

	}

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<OPBillContentsTBL> findBillsContentByBillNumber(Long billNumber)
	    throws RuntimeException
    {
	Query query = em.createNamedQuery("findBillsContentByBillNumber");
	OPBillsTBL billItem = em.find(OPBillsTBL.class, billNumber);
	query.setParameter("bill_number", billItem);
	return (List<OPBillContentsTBL>) query.getResultList();
    }

    @Override
    public OPBillContentsTBL findBillsContentByID(Long id)
    {
	return (OPBillContentsTBL) em.find(OPBillContentsTBL.class, id);
    }

    @Override
    public void persist(OPBillContentsTBL todo)
    {
	em.persist(todo);
	em.flush();

    }

    @Override
    public void update(OPBillContentsTBL todo)
    {
	em.merge(todo);
	em.flush();

    }

}
